import javax.swing.*;
import java.util.Arrays;

public class MyArrayList implements SeqList{
    public int[] elem;
    public int usedSize;

    public static final int MAX_SIZE = 10;
    public MyArrayList() {
        elem = new int[MAX_SIZE];
    }
    @Override
    public void add(int data) {
        //1.判满
        if(isFull()) {
            //满了就扩容
           elem = Arrays.copyOf(elem,2*MAX_SIZE);//扩两倍
            System.out.println("扩容成功");
        }
        //没满
        elem[usedSize] = data;
        usedSize++;
    }

    @Override
    public void add(int pos,int data) {
        //1.判满
        if(isFull()) {
            //满了就扩容
            elem = Arrays.copyOf(elem,2*MAX_SIZE);//扩两倍
        }
        if(pos<0||pos>usedSize) {
            throw new PosException("插入数据的位置非法！");
        }
        for (int i = usedSize-1; i >=pos ; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;

    }

    @Override
    public int indexOf(int toFind) {//查找toFind元素的位置

        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {//查找pos位置的元素
        if(pos<0||pos>usedSize) {
            throw new PosException("查找数据的位置非法！");
        }
        return elem[pos];

    }

    @Override
    public boolean contains(int tofind) {//查找某个元素是否存在
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == tofind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void delete(int key) {//删除第一个key元素
        if(isEmpty()) {
            System.out.println("顺序表为空，无法删除!");
        }
       int index = indexOf(key);
       if(index >= 0) {
           int i = index;
           for (; i < usedSize-1; i++) {
               elem[i] = elem[i+1];
           }
           usedSize--;
           System.out.println("删除成功");
       } else {
           System.out.println("所删除元素不存在");
           return;
       }
        
    }

    @Override
    public int size() {//计算顺序表的大小
        return this.usedSize;
    }

    @Override
    public void clear() {
        usedSize = 0;
    }

    @Override
    public void show() {
        if(isEmpty()) {
            System.out.println("顺序表为空");
            return;
        }
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        if(this.usedSize == MAX_SIZE) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isEmpty() {
        if(usedSize == 0) {
            return true;
        }
        return false;
    }
}
